Guided Practice 7.2

Consider the computation of

(free-vars
  (make-app
    (make-lam
      'x
      (make-app
        'x
        (make-lam
          'y
          (make-app
            'x
            (make-app 'y 'z)))))
    (make-lam
      'y
      (make-app
        'u
        (make-lam
          'z
          (make-app
            'x
            (make-app 'y 'z)))))))

Which of the following will appear as calls to free-vars-in-subexp during this computation?

  1. (free-vars-in-subexp (make-app 'y 'z) (list 'z 'y))
  2. (free-vars-in-subexp (make-app 'y 'z) (list 'x 'y))
  3. (free-vars-in-subexp (make-app 'y 'z) (list 'y 'x))
  4. (free-vars-in-subexp (make-app 'y 'z) (list 'x 'z))

ANSWER

Answer: 1 and 3. According to the invariant, the first argument to free-vars-in-subexp is the list of lambda-variables that the expression occurs inside of. There are two occurences of
(make-app 'y 'z)

in this fredexp. The first is inside

(make-lam 'x
   ...  
  (make-lam 'y 
    ...)) 
and the second is inside
(make-lam 'y  
  ... 
  (make-lam 'z 
    ...)) 

If we look at the code for free-vars-in-subexp, we see that the innermost lambda variable always occurs first. So the right answers are 1 (corresponding to the first occurrence with the lambda variables listed inside out) and 3 (corresponding to the second occurrence with the lambda variables listed inside out).


Last modified: Sat Oct 31 12:30:11 Eastern Daylight Time 2015